package com.alibaba.basic.service.impl;

import com.alibaba.basic.mapper.BaseMapper;
import com.alibaba.basic.query.BaseQuery;
import com.alibaba.basic.service.IBaseService;
import com.alibaba.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    // 注入

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public Long queryCount(BaseQuery baseQuery) {
        return baseMapper.queryCount(baseQuery);
    }

    @Override
    public PageList<T> query(BaseQuery baseQuery) {
        Long count = baseMapper.queryCount(baseQuery);
        if (count == 0) {
            return new PageList<>();
        }
        List<T> queryT = baseMapper.query(baseQuery);
        // 这里获取了T和count数据，直接返回新的对象
        return new PageList<>(count,queryT);
    }

    @Override
    public void patchRemove(List<Long> ids) {
        baseMapper.patchRemove(ids);
    }
}
